const app = getApp()
const API  = require('../../API/api.js')
var utils = require('../../utils/util.js')
Page({
    data: {
        // message: "app.userInfo.username"
        faceUrl: "../resource/images/noneface.png",
        // nickname: "name",
        isMe: true,
        // isFollow: true,
        // fansCounts: 0,
        // followCounts: 0,
        // receiveLikeCounts: 1
    },

    onLoad: function (res) {
        var this_ = this
        var userId = ''
        //判断是不是自己 
        var resId = res.userId;
        var userInfo = app.getGlobaluserInfo()
        var userToken = userInfo.userToken
        if (userInfo == null || userInfo == '' || userInfo == undefined) {
            if (resId != null && resId != '' && resId != undefined) {
                userId = resId;
                this_.setData({
                    isMe: false
                })
            }
        } else {
            var id = userInfo.id
            if (resId != null && userInfo != '' && userInfo != undefined) {
                //判断是不是自己 
                userId = resId

                if (resId === id) {

                    this_.setData({
                        isMe: true

                    })
                } else {
                    this_.setData({
                        isMe: false

                    })
                }


            } else {
                userId = id;

            }
        }

        wx.showLoading(
            { title: "loading" }
        )

        //查询用户信息 
        API.userQuery({
            "userId": userId
        }
        ).then(res =>{
            wx.hideLoading()
            var info = res 
            var faceUrl = ""
            var faceUrl = info.faceImage
            if (faceUrl == null || faceUrl == "") {
                faceUrl = "../resource/images/noneface.png"
                info.faceImage = faceUrl
            } else {
                info.faceImage = app.serverUrl + faceUrl
            }
            this_.setData({
                nickname: info.nickname,
                fansCounts: info.fansCounts,
                followCounts: info.followCounts,
                receiveLikeCounts: info.receiveLikeCounts,
                faceUrl: info.faceImage
            })
        })
       

    },

    followMe: function (res) {
        console.log("followMe")
        console.log(res.target.dataset.followtype)
        if (res.target.dataset.followtype == "0") {
            console.log("已关注")

            this.setData({
                //  message: app.userInfo.username,
                isFollow: false
            })
        } else {
            console.log("未关注")
            this.setData({
                //  message: app.userInfo.username,
                isFollow: true
            })
        }
    },

    uploadVideo: function () {
        utils.uploadVideo()
    },
    logout: function () {
        console.log("注销登录")
        var id = app.getGlobaluserInfo().id
        API.logout({
            userId: id 
        }).then(res=>{
            console.log("用户注销成功");
            wx.removeStorageSync('userInfo')
            wx.redirectTo({
                url: '../home/home',
            })
        })
    }
    ,
    //上传头像
    changeFace: function () {
        var this_ = this
        var userInfo = app.getGlobaluserInfo()
        var userId = userInfo.userId
        var userToken = userInfo.userToken
        wx.chooseImage({
            count: 1, // 默认9
            sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
            sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
            success: function (res) {
                // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
                console.log(res.tempFilePaths[0])
                wx.showLoading({
                    title: '上传中...',
                })

                wx.uploadFile({
                    url: app.serverUrl + '/user/uploadFace',
                    filePath: res.tempFilePaths[0],
                    name: 'file',
                    header: {
                        //设置头 请求数据转换为string  或者不要data 直接在url后面拼接 
                        'content-type': 'application/x-www-form-urlencoded',
                        'userId': userId,
                        'userToken': userToken
                    },
                    formData: {
                        userId: userId
                    },
                    success: function (result) {
                        //上传文件返回的数据data里面是一个String json串,需要解析
                        var data = JSON.parse(result.data);
                        //data 就是解析出来的对象 判断状态 
                        if (data.status == 200) {
                            //成功的状态
                            wx.showToast({
                                title: '上传成功',
                                icon: "success"
                            })
                            // 更新 
                            userInfo.faceImage = data.data;
                            app.setGlobalUserInfo(userInfo)

                            this_.setData({
                                faceUrl: app.serverUrl + userInfo.faceImage
                            })
                        } else {
                            //失败的状态 直接提示
                            wx.showToast({
                                title: data.msg,
                                icon: "none"
                            })
                        }
                    },
                    fail: function (res) {
                        wx.showToast({
                            title: '上传失败',
                        })
                        console.log("上传失败:" + res)

                    }

                })



            }
        })
    }


})